package com.open4cn.simulator.answer.datacolor;

import com.open4cn.simulator.core.answer.AbstractAnswer;
import com.open4cn.simulator.core.answer.AnswerModeEnum;
import com.open4cn.simulator.core.answer.AnswerResponse;
import com.open4cn.simulator.core.ext.DataColorStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description 包含状态切换
 * @Email yinxiaopeng@zj.tech
 * @date 2022-05-03 19:49
 */
public class ChangeSpecularGloss600Answer extends AbstractAnswer {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public ChangeSpecularGloss600Answer() {
        super("changeSpecularGloss600", AnswerModeEnum.SUCCESS);
    }


    public AnswerResponse answer(DataColorStatus dataColorStatus, DataColorStatus.SpecularGlossEnum specularGloss, String instruction) {
        if (!AnswerModeEnum.FAIL.equals(getAnswerMode())) {
            getActions().forEach(action -> {
                action.action(dataColorStatus, specularGloss, instruction);
            });
        }
        AnswerResponse response = getAnswer(dataColorStatus);
        return response;
    }

    /**
     * 获取应答结果
     *
     * @param dataColorStatus
     * @return
     */
    private AnswerResponse getAnswer(DataColorStatus dataColorStatus) {
        switch (getAnswerMode()) {
            case SUCCESS:
                return AnswerResponse.success(success(dataColorStatus));
            case FAIL:
                return AnswerResponse.fail(fail(dataColorStatus));
            case LOOP:
                return AnswerResponse.success(getLoop());
            case INPUT:
                Object input = pollInput();
                if (null != input) {
                    return AnswerResponse.success(input);
                }
            default:
                return AnswerResponse.success(success(dataColorStatus));
        }
    }


    /**
     * 成功应答
     * *INR001xxxx1xxxxs9.2206A9:
     *
     * @param dataColorStatus
     * @return
     */
    private Object success(DataColorStatus dataColorStatus) {
        StringBuilder builder = new StringBuilder();
        builder.append("*")
                .append(dataColorStatus.getCurrentSpecularGloss().getCode())
                .append(dataColorStatus.getCurrentAperture().name())
                .append("R")
                .append(dataColorStatus.getCurrentUV().getCode())
                .append("xxxx1xxxxs9.2206A9:")
                .append("\r\n");
        return builder.toString();
    }


    /**
     * 失败应答
     * *INR001xxEx1xxxxs9.2206A9:
     *
     * @param dataColorStatus
     * @return
     */
    private Object fail(DataColorStatus dataColorStatus) {
        StringBuilder builder = new StringBuilder();
        builder.append("*")
                .append(dataColorStatus.getCurrentSpecularGloss().getCode())
                .append(dataColorStatus.getCurrentAperture().name())
                .append("R")
                .append(dataColorStatus.getCurrentUV().getCode())
                .append("xxEx1xxxxs9.2206A9:")
                .append("\r\n");
        return builder.toString();
    }


}
